<template>
	<view class="body" style="min-height: 1400rpx;">
		<!-- <view class="header">我的订单</view> -->
		<view style="height: 10rpx;"></view>
		<view v-show="is_empty" style="margin-top: 200rpx;">
			<image style="width: 750rpx;" :src="'https://s1.ax1x.com/2022/08/01/vAIOr4.png'" mode="aspectFit"> </image>
		</view>
		<view class="outer-box" v-for="(item,index) in orders">
			<view style="width: 100%;height: 60rpx;">
				<text class="title-font">订单编号: </text>
				<text style="font-family: verdana;font-size: 26rpx;"> {{item._id}}</text>
			</view>
			<view v-if="orderGoodsList[index]==null||orderGoodsList[index].length==0">
				<text class="title-font">物品名: </text>
				<text style="font-family: verdana;font-size: 26rpx;"> {{item.title}}</text>
			</view>
			<view v-else>
				<myOrderList :goodsList="orderGoodsList[index]"></myOrderList>
			</view>
			<view>
				<uni-row>
					<uni-col>
						<text class="title-font">下单时间: </text>
						<uni-dateformat :date="item.create_time" style="font-size: 26rpx;"></uni-dateformat>
					</uni-col>
				</uni-row>
				<uni-row>
					<uni-col :span="18">
						<text class="title-font">实际回收价格: </text>
						<text v-if="item.price==null">
							<text class="yuan-font">￥</text><text class="price-font">
								待评估
							</text>
						</text>
						<text v-else>
							<text class="yuan-font">￥</text><text class="price-font">
								{{convert(item.price).integer}}.
							</text>
							<text class="price-font">
								{{convert(item.price).point}}
							</text>
						</text>
					</uni-col>
					<uni-col :span="6">
						<view v-if="status==0">
							<button class="btn" @click="confirm(item)">确认回收</button>
						</view>
						<view v-else-if="status==1">
							<button class="btn" @click="cancle(item._id)">不想卖了</button>
						</view>
					</uni-col>
				</uni-row>
			</view>
		</view>
	</view>
</template>

<script>
	import myOrderList from "@/components/myOrderList/myOrderList.vue"

	import {
		GetOrderObj
	} from '@/common/utils/getCurrentUserOrder.js'
	const getOrderObj = new GetOrderObj()

	const db = uniCloud.database()
	const dbCmd = db.command



	export default {
		data() {
			return {
				orders: [],
				orderGoodsList: [],
				is_empty: true,
				index: 0,
				status: 0,
				refresh: true
			}
		},
		onLoad(option) {
			this.status = option.index
			this.index = option.index
			this.getOrderGoodsList()
		},
		methods: {
			convert(money) {
				return {
					integer: Math.floor(money / 100),
					point: money % 100
				}
			},
			async updateOrder(id, desc, status) {
				let flag = true
				let res = await db.collection('uni-id-base-order').where({
					_id: id
				}).update({
					status: status,
					is_handle: false,
					cancel_desc: desc
				}).catch(e => {
					flag = false
				})

				return flag
			},
			getOrderGoodsList() {
				this.is_empty = false
				this.orders = this.$store.state.orders
				this.orderGoodsList = this.$store.state.orderGoodsList
				console.log(this.orderGoodsList[0]);
				if (this.orders.length == 0) {
					this.is_empty = true
				}
			},
			//取消  修改订单状态 is_handle status
			cancle(id) {
				uni.showModal({
					title: '不想出售理由',
					editable: true,
					success: (res) => {
						if (res.confirm) {
							let flag = this.updateOrder(id, res.content, 3)
							if (flag) {
								uni.showToast({
									title: '取消审核中...',
									icon: "success",
								})

								setTimeout(() => {
									uni.hideToast()
									this.reload(parseInt(this.index))
								}, 3000)
							} else {
								uni.showToast({
									title: '取消失败，请重试',
									icon: "warnning",
									duration: 800
								})
							}
						}
					}
				})
			},
			confirm(item) {
				if (item.price == null) {
					uni.showToast({
						title: '请等待估价完成',
						duration: 1000,
						icon: "none"
					});
					return
				}

				uni.showModal({
					title: '确认吗？',
					editable: false,
					success: (res) => {
						if (res.confirm) {
							let flag = this.updateOrder(item._id, res.content, 1)
							if (flag) {
								uni.showToast({
									title: '确认成功',
									icon: "success",
								})

								setTimeout(() => {
									uni.hideToast()
									this.reload(parseInt(this.index))
								}, 3000)
							} else {
								uni.showToast({
									title: '确认失败，请重试',
									icon: "warnning",
									duration: 800
								})
							}
						}
					}
				})
			},
			async reload(index) {
				uni.showToast({
					title: '加载中...',
					mask: true,
					icon: 'loading'
				})
				if (index == 1 || index == 3) {
					await getOrderObj.getOrderGoodsList(index, true)
				} else {
					await getOrderObj.getOrderGoodsList(index, false)
				}

				uni.hideToast();
				uni.redirectTo({
					url: '/pages/order/myorder/myorder?index=' + index
				})

			},

		},
		components: {
			myOrderList
		}
	}
</script>

<style>
	.btn {
		min-width: 140rpx;
		height: 60rpx;
		border: gainsboro 1px solid;
		color: #a3a3a3;
		font-size: 28rpx;
		border-radius: 18rpx;
		line-height: 60rpx;
	}

	.is_empty {
		width: 710rpx;
		height: 600rpx;
	}

	.title-font {
		font-size: Tahoma;
		font-weight: bold;
	}

	.yuan-font {
		color: #FF5000;
		font-size: 32rpx;
	}


	.header {
		width: 750rpx;
		height: 105rpx;
		background-color: #f9f9fc;
		line-height: 105rpx;
		text-align: center;
		font-weight: bold;
		font-size: 18px;
		margin-bottom: 10rpx;
	}
</style>